﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.DTO;
using QLBH.Base;
using QLBH.Model.Controller;

namespace Module.Category
{
    public partial class RegisterShopInformation : Form
    {
        private eDialogType dialogType;

        public eDialogType DialogType
        {
            get { return dialogType; }
            set { dialogType = value; }
        }
        private CuaHangDTO newShop;

        public CuaHangDTO NewShop
        {
            get { return newShop; }
            set 
            { 
                newShop = value;
                if (dialogType==eDialogType.DL_EDIT)
                {
                    txtShopName.Text = newShop.TenCH;
                    txtAddress.Text = newShop.DiaChi;
                    txtEmail.Text = newShop.Email;
                    txtPhoneNumber.Text = newShop.DienThoai;
                }
            }
        }

        public RegisterShopInformation()
        {
            InitializeComponent();
            newShop = new CuaHangDTO();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "Bitmap Image|*.bmp|PNG Image|*.png|JPEG Image|*.jpg";
            openFileDlg.Multiselect = false;
            if (openFileDlg.ShowDialog()==DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(openFileDlg.FileName);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (IsValidValue())
            {
                try
                {
                    if (dialogType == eDialogType.DL_ADD)
                    {
                        AddShopInformation();
                    }
                    else
                    {
                        UpdateShopInformation();
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.InnerException.Message, "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
                }
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void UpdateShopInformation()
        {
            UpdateInformationFromControl();

            ShopController.Instance.Edit(newShop.ToEntity());
        }

        private void AddShopInformation()
        {
            UpdateInformationFromControl();

            ShopController.Instance.Add(newShop.ToEntity());
        }
        private void UpdateInformationFromControl()
        {
            newShop.DiaChi = txtAddress.Text;
            newShop.DienThoai = txtPhoneNumber.Text;
            newShop.Email = txtEmail.Text;
            newShop.TenCH = txtShopName.Text;
        }

        private bool IsValidValue()
        {
            // Shop Name
            try
            {
                DataInputHelper.CheckTextData(txtShopName.Text, "Tên cửa hàng");

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtShopName.SelectAll();
                txtShopName.Focus();
                return false;
            }
            // Phone Number
            try
            {
                DataInputHelper.CheckPhoneNumber(txtPhoneNumber.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPhoneNumber.SelectAll();
                txtPhoneNumber.Focus();
                return false;
            }
            //Email
            try
            {
                DataInputHelper.CheckEmailData(txtEmail.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail.SelectAll();
                txtEmail.Focus();
                return false;
            }
            return true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void RegisterShopInformation_KeyPress(object sender, KeyPressEventArgs e)
        {

        }
    }
}
